CONTENTS | INDEX | PREV | NEXT
fsetpos
NAME
fsetpos - set position within file pointer (nearly equivalent to
fseek absolute)
SYNOPSIS
#include <stdio.h>
int error = fsetpos(fp, &pos);
FILE *fp;
fpos_t pos;
FUNCTION
fsetpos() is a nearly useless call that is essentially the same
as fseek(fp, (long)pos, 0); fsetpos() seeks within a file pointer
to the absolute position specified by an fpos_t type. The
address of an fpos_t object is passed to fsetpos().
Normally one saves the current seek position into an fpos_t
type using the fgetpos() function, then seeks back using
the fsetpos() function. In this way the programmer need not make
any direct reference to the contents of the fpos_t type.
EXAMPLE
/*
* get a line, save current position, get rest of file, go back
* to saved position, retrieve line again and print again.
*/
#include <stdio.h>
main(ac, av)
int ac;
char **av;
{
FILE *fp;
fpos_t save_pos;
int count;
char buf[256];
if (ac == 1) {
puts("Expected textfile argument");
exit(1);
}
fp = fopen(av[1], "r");
if (fp == NULL) {
printf("Unable to open %sn", av[1]);
exit(1);
}
for (count = 0; fgets(buf, sizeof(buf), fp); ++count) {
if (count == 0) /* just before second line */
fgetpos(fp, &save_pos);
fprintf(stdout, "%-3d: %s", count + 1, buf);
}
if (count < 2) {
puts("not enough lines in file for example!");
exit(1);
}
puts("--end of file, now seeking back to line 2--");
fsetpos(fp, &save_pos);
if (fgets(buf, sizeof(buf), fp) == NULL) {
puts("error!");
exit(1);
}
fprintf(stdout, "%-3d: %s", 2, buf);
fclose(fp);
return(0);
}
INPUTS
FILE *fp; file pointer to seek
fpos_t *pos; pointer to fpos_t type previously initialized
by a fgetpos() call.
RESULTS
int error; 0 if no error, < 0 if error
SEE ALSO
ftell, fsetpos, fseek, rewind